package com.luhongzhe.gameview;

import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.luhongzhe.Luhongzhe;
import com.luhongzhe.R;
import com.luhongzhe.tool.GravityTool;
import com.luhongzhe.tool.PathParser;
import com.luhongzhe.tool.PhysicalTool;

import data.ObjectDrawable;

/**
 * @author 	luhongzhe
 * @email  	azhe0660@gmail.com
 * @date   	2011-10-8
 */

public class CommonGameView extends SurfaceView 
	implements SurfaceHolder.Callback, Runnable
{
	protected Paint defaultPaint;
	protected Paint textPaint;
	
	protected boolean hasInit;
	protected boolean hasSucceed;
	
	private Paint fingerPaint;
	private Path fingerPath;
	private boolean fingerCutEnd;
	
	boolean mLoop;
	SurfaceHolder mSurfaceHolder;
	
	GameViewHolder holder;
	
	public CommonGameView(Context context, GameViewHolder holder)
	{
		super(context);
		this.holder = holder;
		mSurfaceHolder = this.getHolder();  
		mSurfaceHolder.addCallback(this);  

	}
	
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		setMeasuredDimension(500, 500);
	}
	
	protected void drawGame(Canvas canvas)
	{
		
	}
	
	protected void draw()
	{
		if (!hasInit)
		{
			init();
		}
		
		Canvas canvas = mSurfaceHolder.lockCanvas();
		canvas.drawColor(Color.BLACK);
		
		canvas.drawText("这是哪一关呀", 50, 50, textPaint);
		
		drawGame(canvas);
		
		if (fingerCutEnd)
		{
			fingerPaint.setAlpha(Math.max(fingerPaint.getAlpha() - 5, 0));
			canvas.drawPath(fingerPath, fingerPaint);
		}
		else
		{
			fingerPaint.setAlpha(0xFF);
			canvas.drawPath(fingerPath, fingerPaint);
		}
		
		if (hasSucceed)
		{
			canvas.drawText("Bingo!", 200, 200, textPaint);
		}
		
//		invalidate();
		mSurfaceHolder.unlockCanvasAndPost(canvas);
	}
	
	protected void onSucceed()
	{
		hasSucceed = true;
		holder.success();
	}

	@Override
	protected void onDetachedFromWindow () 
	{
		super.onDetachedFromWindow();
	}
	
	protected float mX, mY;
	protected static final float TOUCH_TOLERANCE = 4;
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
	    String action = "";
	    switch (event.getAction()) {
	    case MotionEvent.ACTION_DOWN:
	        action = "ACTION_DOWN";
	       onTouchDown(event);
	        break;
	    case MotionEvent.ACTION_MOVE:
	    	action = "ACTION_MOVE";
	    	onTouchMove(event);
	    	break;
	    case MotionEvent.ACTION_UP:
	    case MotionEvent.ACTION_CANCEL:
	        action = "ACTION_UP";
	        onTouchUp(event);
	        break;
	    }
	    
//	    return super.onTouchEvent(event);
	    return true;
	}
	
	protected void onTouchDown(MotionEvent event)
	{
		float x = event.getX();
		float y = event.getY();
		
		hasSucceed = false;
        
        fingerCutEnd = false;
        fingerPath = new Path();
        fingerPath.moveTo(x, y);
        mX = x;
        mY = y;
	}
	
	protected void onTouchMove(MotionEvent event)
	{
		float x = event.getX();
		float y = event.getY();
		float dx = Math.abs(x - mX);
		float dy = Math.abs(y - mY);
		if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
		{
			fingerPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
			mX = x;
			mY = y;
    	}
	}
	
	protected void onTouchUp(MotionEvent event)
	{
		float x = event.getX();
		float y = event.getY();
		fingerPath.lineTo(x, y);
		fingerCutEnd = true;
		mX = -1;
		mY = -1;
	}
	
	protected void init()
	{
		defaultPaint = new Paint();
		defaultPaint.setColor(Color.GRAY);
		
		textPaint = new Paint();
		textPaint.setColor(Color.RED);
		textPaint.setTextSize(30);
		
		fingerPaint = new Paint();
		fingerPaint.setColor(Color.YELLOW);
		fingerPaint.setStyle(Paint.Style.STROKE);
		fingerPaint.setStrokeJoin(Paint.Join.ROUND);
		fingerPaint.setStrokeCap(Paint.Cap.ROUND);
		fingerPaint.setStrokeWidth(12);
		fingerPath = new Path();
		
		hasInit = true;
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		new Thread(this).start();
		mLoop = true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
		mLoop = false;
	}

	@Override
	public void run()
	{
		while(mLoop)  
        {  
            try  
            {  
            	draw();  
                Thread.sleep(10);  
            }  
            catch(Exception e)  
            {  

            }  
        }  
	}
}
